﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Galaktika.BI.Silverlight.Controls.BallGame
{
    public delegate void BubbleClickEventHandler(Bubble bubble);
    public delegate void BubblesRemovedEventHandler(int count);
    public delegate void GameOverEventHandler(Board board);

 
    public class Bubble : IDisposable
    {
        // Fields
        private Canvas canvas;
        private int colorNr;
        private Ellipse ellipse;
        private int posX;
        private int posY;
        private int size;

       
 
        // Events
        public event BubbleClickEventHandler BubbleClick;

        // Methods
        public Bubble(Canvas canvas, int size, int colorNr, int x, int y)
        {
            this.canvas = canvas;
            this.size = size;
            this.colorNr = colorNr;
            this.ellipse = new Ellipse();
            this.ellipse.Width = size;
            this.ellipse.Height = size;
            this.ellipse.Fill = this.GetBrush(colorNr);
            canvas.Children.Add(this.ellipse);
            this.PosX = x;
            this.PosY = y;
            this.ellipse.MouseLeftButtonUp += new MouseButtonEventHandler(this.ellipse_MouseLeftButtonUp);
        }

        public void Dispose()
        {
            if ((this.ellipse != null) && (this.canvas != null))
            {
                if (this.canvas.Children.Contains(this.ellipse))
                {
                    this.canvas.Children.Remove(this.ellipse);
                }
                this.ellipse = null;
                this.canvas = null;
            }
        }

        private void ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.BubbleClick != null)
            {
                this.BubbleClick(this);
            }
        }

        protected RadialGradientBrush GetBrush(int color)
        {
            Color color2;
            RadialGradientBrush brush = new RadialGradientBrush();
            switch (this.colorNr)
            {
                case 0:
                    color2 = Color.FromArgb(0xff, 0xff, 0, 0);
                    break;

                case 1:
                    color2 = Color.FromArgb(0xff, 0, 0, 0xff);
                    break;

                case 2:
                    color2 = Color.FromArgb(0xff, 0, 0, 0);
                    break;
                case 3:
                    color2 = Color.FromArgb(0xff,128, 128, 0);
                    break;
                case 4:
                    color2 = Colors.Green;
                    break;
                case 5:
                    color2 = Colors.Orange;
                    break;
                case 6:
                    color2 = Colors.Yellow;
                    break;
                case 7:
                    color2 = Colors.Purple;
                    break;
                case 8:
                    color2 = Colors.Cyan;
                    break;
                case 9:
                    color2 = Colors.Brown;
                    break;
                default:
                    color2 = Color.FromArgb(0xff, 0xff, 0xff, 0xff);
                    break;
            }
            Point point = new Point();
            point.X = 0.3;
            point.Y = 0.3;
            brush.GradientOrigin = point;
            brush.RadiusX = 0.5;
            brush.RadiusY = 0.5;
            GradientStop stop = new GradientStop();
            stop.Color = Color.FromArgb(0xff, 0xff, 0xff, 0xff);
            stop.Offset = 0.0;
            brush.GradientStops.Add(stop);
            GradientStop stop2 = new GradientStop();
            stop2.Color = color2;
            stop2.Offset = 0.75;
            brush.GradientStops.Add(stop2);
            return brush;
        }

        // Properties
        public int ColorNr
        {
            get
            {
                return this.colorNr;
            }
        }

        public int PosX
        {
            get
            {
                return this.posX;
            }
            set
            {
                this.posX = value;
                this.ellipse.SetValue(Canvas.LeftProperty,(double) (value * this.size));
            }
        }

        public int PosY
        {
            get
            {
                return this.posY;
            }
            set
            {
                this.posY = value;
                this.ellipse.SetValue(Canvas.TopProperty,(double)(value * this.size));
            }
        }
    }

    public class BubblesArea
    {
        // Fields
        private List<Bubble> bubbles = new List<Bubble>();

        // Methods
        public BubblesArea(int width, int height)
        {
        }

        public Bubble Find(int x, int y)
        {
            foreach (Bubble bubble in this.bubbles)
            {
                if ((bubble.PosX == x) && (bubble.PosY == y))
                {
                    return bubble;
                }
            }
            return null;
        }

        // Properties
        public Bubble this[int x, int y]
        {
            get
            {
                return this.Find(x, y);
            }
            set
            {
                if (value == null)
                {
                    Bubble item = this.Find(x, y);
                    if (item != null)
                    {
                        item.Dispose();
                        this.bubbles.Remove(item);
                    }
                }
                else
                {
                    if (this.Find(x, y) != null)
                    {
                        throw new SystemException(String.Format(Localization.Exc_PositionAlreadyTaken,x,y));
                    }
                    if (!this.bubbles.Contains(value))
                    {
                        this.bubbles.Add(value);
                    }
                    value.PosX = x;
                    value.PosY = y;
                }
            }
        }
    }



}
